package com.bc.web.controller.admin;

import java.util.List;

import com.fs.common.extend.FSController;
import com.bc.web.model.Channel;
import com.bc.web.model.ChannelFee;
import com.bc.web.model.Dictionary;
import com.bc.web.model.FeeModel;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;

public class ChannelFeeControlle extends FSController {

	public void search() {
		Page<ChannelFee> items = ChannelFee.dao.search(super.getSearchCondition());
		List<Channel> channelList = Channel.dao.selectAll();
		List<FeeModel> feeModelList = FeeModel.dao.selectAll();
		List<Dictionary> payTypeList = Dictionary.dao.selectByParentKey("payType");
		setAttr("channelList", channelList);
		setAttr("feeModelList", feeModelList);
		setAttr("payTypeList", payTypeList);
		setPageResultToRequest("items", items);
		render("fee/channel/channelFeeList.html");
	}
	
	public void addPage() {
		List<FeeModel> feeModelList = FeeModel.dao.selectAll();
		List<Dictionary> payTypeList = Dictionary.dao.selectByParentKey("payType");
		List<Channel> channelList = Channel.dao.selectAll();
		setAttr("feeModelList", feeModelList);
		setAttr("payTypeList", payTypeList);
		setAttr("channelList", channelList);
		render("fee/channel/channelFeeAdd.html");
	}
	
	public void updatePage() {
		ChannelFee channelFee = ChannelFee.dao.findById(getPara("id"));
		setAttr("channelFee", channelFee);
		List<FeeModel> feeModelList = FeeModel.dao.selectAll();
		List<Dictionary> payTypeList = Dictionary.dao.selectByParentKey("payType");
		List<Channel> channelList = Channel.dao.selectAll();
		setAttr("feeModelList", feeModelList);
		setAttr("payTypeList", payTypeList);
		setAttr("channelList", channelList);
		render("fee/channel/channelFeeUpdate.html");
	}

	@Before(Tx.class)
	public void add() {
		ChannelFee channelFee = getModel(ChannelFee.class, true);
		ChannelFee c = ChannelFee.dao.selectChannelFee(channelFee.getChannelCode(), channelFee.getPayTypeCode());
		if (c != null) {
			renderFail("费率已存在.");
		}else {
			channelFee.save();
			renderSuccess();
		}
	}

	@Before(Tx.class)
	public void update() {
		ChannelFee channelFee = getModel(ChannelFee.class, true);
		channelFee.update();
		renderSuccess();
	}

	@Before(Tx.class)
	public void delete() {
		ChannelFee.dao.deleteById(getPara("id"));
		renderSuccess(false);
	}

}
